package com.hmall.order.task;

import com.hmall.common.client.ItemClient;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.service.OrderDetailService;
import com.hmall.order.service.impl.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/*
 *   Description OrderCancelTask
 *   @author zhangyan
 *   @time   2024/11/15
 */
@Component
@Slf4j
public class OrderCancelTask {
    @Autowired
    private ItemClient itemClient;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 取消订单定时任务
     */
//    @Scheduled(cron = "0 */1 * * * ? ")
    @Transactional
    public void cancelOrder(){
        log.info("取消订单定时任务开始执行");
        //查询订单状态为1的订单,并且订单支付超过30分钟的订单
        List<Order> orders = orderService.selectOrderByStatusAndTime();
        if (orders!=null && orders.size()>0){
                for (Order order : orders){
                    //根据订单id查询订单详情
                 OrderDetail orderDetail = orderDetailService.findByOrderId(order.getId());
                 if (orderDetail!=null){
                     //调用用户服务更新库存
                     itemClient.rebackStock(orderDetail.getItemId(),orderDetail.getNum());
                     //修改订单状态为5(已取消)
                     orderService.updateStatusById(order.getId(),5);
                 }

                }
        }




       }

    }

